package org.zakariya.stickyheadersapp.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import org.zakariya.stickyheaders.StickyHeaderLayoutManager;
import org.zakariya.stickyheaders.Utils;
import org.zakariya.stickyheaders.model.ItemModel;
import org.zakariya.stickyheadersapp.ResourceTable;
import org.zakariya.stickyheadersapp.adapter.PagedScrollingAdapter;

import java.util.List;
/**
 * PagedScrollingAbilitySlice
 *
 * @since 2021-08-20
 */
public class PagedScrollingAbilitySlice extends AbilitySlice {
    private MyRun myRun;
    private ProgressBar progressBar;
    private ProgressBar mainProgressBar;
    private ListContainer listContainer;
    private TaskDispatcher taskDispatcher;
    private List<ItemModel> dataList;
    private List<ItemModel> dataListTwo;
    private PagedScrollingAdapter adapter;
    private Text end;
    private Revocable revocable;
    private int value = 0;
    private int isAdd = 0;
    private int endAdd = 2;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_paged_scrolling);
        initComponent();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    private void initComponent() {
        myRun = new MyRun();
        Image image = (Image) findComponentById(ResourceTable.Id_icon_back);
        image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });
        Text headerText = (Text) findComponentById(ResourceTable.Id_paged_text);
        Text indexText = (Text) findComponentById(ResourceTable.Id_index_text);
        mainProgressBar = (ProgressBar) findComponentById(ResourceTable.Id_progressBar);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_paged_callbacks);
        dataList = Utils.getPagedList(1, 20, true);
        dataListTwo = Utils.getPagedList(2, 20, true);
        adapter = new PagedScrollingAdapter(this, dataList);
        listContainer.setItemProvider(adapter);
        StickyHeaderLayoutManager headerDecor = new StickyHeaderLayoutManager(listContainer, null, headerText, indexText);
        headerDecor.setDataList(dataList,true);
        listContainer.addScrolledListener(new Component.ScrolledListener() {
            @Override
            public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {
                if (listContainer.getLastVisibleItemPosition() == listContainer.getChildCount() - 1) {
                    progressBar = (ProgressBar) listContainer.getComponentAt(listContainer.getChildCount() - 1).findComponentById(ResourceTable.Id_progressBar);
                    end = (Text) listContainer.getComponentAt(listContainer.getChildCount() - 1).findComponentById(ResourceTable.Id_item_text);
                    myRun.run();
                }

            }
        });
        myRun.run();
    }

    private void setProgress() {
        if (taskDispatcher == null) {
            taskDispatcher = this.getUITaskDispatcher();
        }
        revocable = taskDispatcher.delayDispatch(myRun, 100);
    }

    private class MyRun implements Runnable {
        @Override
        public void run() {
            if (isAdd == endAdd) {
                progressBar.setVisibility(Component.HIDE);
                end.setVisibility(Component.VISIBLE);
                end.setText("No More Item");
                return;
            }
            if (progressBar != null && progressBar.getProgress() != 100 && progressBar.getVisibility() == Component.VISIBLE) {
                progressBar.setProgressValue(value += 10);
            }
            if (mainProgressBar.getVisibility() == Component.VISIBLE) {
                mainProgressBar.setProgressValue(value += 10);
            }
            if (value < 100) {
                setProgress();
            } else {
                value = 0;
                if (mainProgressBar.getProgress() == 100) {
                    mainProgressBar.setVisibility(Component.HIDE);
                    listContainer.setVisibility(Component.VISIBLE);
                    revocable.revoke();
                }
                if (progressBar != null && progressBar.getProgress() == 100) {
                    if (isAdd == 0) {
                        dataListTwo.remove(listContainer.getChildCount() - 1);
                        dataList.clear();
                        dataList.addAll(dataListTwo);
                        adapter.notifyDataChanged();
                    } else if (isAdd == 1) {
                        progressBar.setVisibility(Component.HIDE);
                        end.setVisibility(Component.VISIBLE);
                        end.setText("No More Item");
                    }
                    isAdd++;
                }
            }
        }
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
